<template>
  <el-dialog
    title="新增货物"
    :visible.sync="dialogVisible"
    width="1050px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    custom-class="custom-class-line"
    @close="closeDialog">
    <Basic :data="form" ref="basic"></Basic>
    <div class="text-center">
      <el-button type="primary" style="width:100px" @click="confirmAdd">确认新增</el-button>
      <el-button style="width:100px" @click="closeDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Basic from './basic/basic.vue'
import { randomString,arrSubtraction } from "@/utils/zito"
export default {
  name: 'add',
  components: {
    Basic,
  },
  data() {
    return {
      dialogVisible: false,
      activeName: 'basic',
      form: {
        customerName: '',
        customerType: '',
        customerNature: '',
        industry: '',
        officePhone: '',
        postalCode: '',
        contacts: '',
        post: '',
        relatedAccessories: [],
        businessRegistrationNo: '',
        legalPersonName: undefined,
        corporateIDCard: undefined,
        registeredCapital: undefined,
        establishmentTime: undefined,
        mailAddress: undefined,
        mailDetailedAddress: undefined,
        owningVehicle: undefined,
        externalVehicles: undefined,
        // 合同信息
        contractInfoList: [
          {
            uuid: randomString(32)
          }
        ],
        addressList: [
          {
            uuid: randomString(32)
          }
        ]
      },
      refList: [
        'basic',
      ],
    };
  },
  methods: {
    showDialog() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.$emit('close')
    },
    async confirmAdd() {
      const valid = await this.$refs[this.activeName].handleValidate()
      if (valid) {
        let promiseArray = [];
        const remainRefList = arrSubtraction(this.refList, [this.activeName])
        for(let i = 0;i<remainRefList.length;i++){
          promiseArray.push(new Promise(async(resolve) => {
            const result = await this.$refs[remainRefList[i]].handleValidate()
            if (result) resolve(true)
            else resolve(false)
          }))
        }
        const resultArray = await Promise.all(promiseArray)
        const resultAll = resultArray.every(v => v)
        if (resultAll) {

        } else {
          this.$message.error('请完善其他信息')
        }
      }
    }
  }
};
</script>